package com.my.xl.salary.billdemo.service;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

@Service       //数据的加密与解密   //base64算法,解密，加密
public class AESToolService {

    private byte[] initVector={0x32, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,0x38, 0x27, 0x36, 0x35, 0x33, 0x23, 0x32, 0x31};

    //加密
    public String base64Encode(String str) {
        BASE64Encoder baseEncode = new BASE64Encoder();
        return baseEncode.encode(str.getBytes());//加密
    }

    //解密
    public String base64Decode(String str) {
        BASE64Decoder baseDecoder = new BASE64Decoder();
        try {
            return new String(baseDecoder.decodeBuffer(str));//解密
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }





    /*//加密数据
    public String encrypt2(String content, String key) throws Exception {
        if (key == null) {
            throw new IllegalArgumentException("Key cannot be null!");
        }
        String encrypted = null;
        if(content!=null&&!content.equals("")){
            byte[] keyBytes = key.getBytes();
            if (keyBytes.length != 32 && keyBytes.length != 24&& keyBytes.length != 16) {
                throw new IllegalArgumentException("Key length must be 128/192/256 bits!");
            }
            byte[] encryptedBytes = null;
            encryptedBytes = this.encrypt(content.getBytes(), keyBytes, initVector);
            encrypted = new String(Hex.encode(encryptedBytes));
        }
        return encrypted;
    }

    //解密数据
    public String decrypt2(String content, String key) throws Exception {
        if (key == null) {
            throw new IllegalArgumentException("Key cannot be null!");
        }
        String decrypted = null;
        if(content!=null&&!content.equals("")){
            byte[] encryptedContent = Hex.decode(content);
            byte[] keyBytes = key.getBytes();
            byte[] decryptedBytes = null;
            if (keyBytes.length != 32 && keyBytes.length != 24&& keyBytes.length != 16) {
                throw new IllegalArgumentException("Key length must be 128/192/256 bits!");
            }
            decryptedBytes = this.decrypt(encryptedContent, keyBytes, initVector);
            decrypted = new String(decryptedBytes);
        }
        return decrypted;
    }


    private byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception {
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key),iv);
        aes.init(true, ivAndKey);
        return cipherData(aes, plain);
    }

    private byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception {
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key),iv);
        aes.init(false, ivAndKey);
        return cipherData(aes, cipher);
    }

    private byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws Exception {
        int minSize = cipher.getOutputSize(data.length);
        byte[] outBuf = new byte[minSize];
        int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
        int length2 = cipher.doFinal(outBuf, length1);
        int actualLength = length1 + length2;
        byte[] result = new byte[actualLength];
        System.arraycopy(outBuf, 0, result, 0, result.length);
        return result;
    }

    public String getKey(String appid) {
        return "fdafsji3829AENG7hfdihf66112374jr";
    }*/










}
